package xyz.hubery.controller;

import com.baomidou.mybatisplus.core.metadata.OrderItem;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.context.SecurityContext;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import xyz.hubery.domain.CashRecharge;
import xyz.hubery.domain.CashRechargeAuditRecord;
import xyz.hubery.model.BuyGCNParam;
import xyz.hubery.model.CashTradeVo;
import xyz.hubery.model.R;
import xyz.hubery.service.CashRechargeService;

@RestController
@RequestMapping("/cashRecharges")
@Api("GCN控制器")
public class CashRechargeController {

    @Autowired
    private CashRechargeService cashRechargeService;

    @GetMapping("/records")
    @ApiOperation("分页查询GCN充值记录")
    public R<Page<CashRecharge>> findByPage(Long userId,
                                            String userName,
                                            String mobile,
                                            Byte status,
                                            Integer numMin,
                                            Integer numMax,
                                            String startTime,
                                            String endTime
            , Page<CashRecharge> page) {
        Page<CashRecharge> cashRechargePage = cashRechargeService.findPage(userId, userName, mobile, status, numMin, numMax, startTime, endTime,  page);
        return R.ok(cashRechargePage);
    }

    @PostMapping("/buy")
    @ApiOperation("现金充值购买GCN")
    public R<CashTradeVo> buy(@RequestBody @Validated BuyGCNParam param) {
        // 获取当前用户
        Long userId= Long.valueOf(SecurityContextHolder.getContext().getAuthentication().getPrincipal().toString());
        CashTradeVo vo = cashRechargeService.buy(userId,param);
        return  R.ok(vo);
    }


    @GetMapping("/user/records")
    @ApiOperation("分页查询用户的充值记录")
    public R<Page<CashRecharge>> findCashRechargePage(Page<CashRecharge> page,Byte status) {
        Long userId = Long.valueOf(SecurityContextHolder.getContext().getAuthentication().getPrincipal().toString());
        page.addOrder(OrderItem.desc("last_update_time"));
        Page<CashRecharge> cashRechargePage = cashRechargeService.findCashRechargePage(page,userId,status);
        return R.ok(cashRechargePage);
    }

    @PostMapping("/cashRechargeUpdateStatus")
    @ApiOperation("场外交易充值审核")
    public R updateStatus(@RequestBody CashRechargeAuditRecord cashRechargeAuditRecord) {
        Long adminId = Long.valueOf(SecurityContextHolder.getContext().getAuthentication().getPrincipal().toString());
        boolean b = cashRechargeService.updateStatus(adminId,cashRechargeAuditRecord,"recharge_into");
        return b?R.ok():R.fail("审核失败");
    }

}
